import { _decorator, Component, Node } from 'cc';
import DatasSpace from './Tool/DatasSpace';
import { WECHAT } from 'cc/env';
const { ccclass, property } = _decorator;
//因为子域跟主域代码不能互相引用，因此定义一个表，分别复制到两边当成一个来使用。
const Consts = {
    OpenDataKeys: {
       LevelKey: "s",
    },
    DomainAction: {
        ShowFriendLevel: "ShowFriendLevel", //展示好友排行榜
    },
}
@ccclass('lanuch')
export class lanuch extends Component {
    @property(Node)
    wxSubContextView:Node=null;
    @property(Node)
    phb:Node=null;
    start() {

    }

    update(deltaTime: number) {
        
    }

    initUserInfoButton () {
        // 检查是否在小游戏环境中，优先使用 tt（抖音），然后是 wx（微信）
        const miniGameApi = (typeof tt !== 'undefined') ? tt : (typeof wx !== 'undefined') ? wx : null;
        
        if (!miniGameApi) {
            return;
        }
        
        var self=this;
        //获取屏幕高度
        let systemInfo = miniGameApi.getSystemInfoSync();
        let width = systemInfo.windowWidth;
        let height = systemInfo.windowHeight;
        var  kvDataList=new Array();
        kvDataList.push({
            key: "s",
            value:DatasSpace.Instance.level.toString(),
        });
        miniGameApi.setUserCloudStorage({
            KVDataList: kvDataList
        });
        
        // 使用动态获取的 window 对象
        const globalMiniGame = (window as any).tt || (window as any).wx || miniGameApi;
        
        globalMiniGame.getSetting({
            success (res) {
                //console.log(res.authSetting);
                if (res.authSetting["scope.userInfo"]) {
                    if(WECHAT){
                        miniGameApi.postMessage({ action: Consts.DomainAction.ShowFriendLevel, message: 'showFriendLevelRank' });
                    }
                    miniGameApi.getUserInfo({
                        success(res1){
                            // //console.log(res1);
                            // let userInfo = res1.userInfo;
                            // //console.log("玩家信息："+userInfo.nickName+"-"+userInfo.avatarUrl);
                            // //描绘昵称
                            // var  name=userInfo.nickName;
                            // if(userInfo.nickName.length>5){
                            //     name=userInfo.nickName.substring(0,5);
                            // }
                            // if(self.nickName){
                            //     self.nickName.string = name;
                            // }
                            // cc.loader.load({url: userInfo.avatarUrl, type: 'png'}, (err, texture) => {
                            //     if (err) {
                            //         console.error(err);
                            //         return;
                            //     }
                            //     if(self.avatar){
                            //         self.avatar.spriteFrame = new cc.SpriteFrame(texture);
                            //         self.avatar.node.width=69;
                            //         self.avatar.node.height=69;
                            //     }
                            // });
                        },
                        fail(info){
                            console.log("获取玩家信息失败："+info);
                        }
                    }
                    )
                  
                }else {
                    let button = miniGameApi.createUserInfoButton({
                        type: 'text',
                        text: '',
                        style: {
                            left: 0,
                            top: 0,
                            width: width,
                            height: height,
                            lineHeight: 40,
                            backgroundColor: '#00000000',
                            color: '#00000000',
                            textAlign: 'center',
                            fontSize: 10,
                            borderRadius: 4
                        }
                    });
                    button.onTap((res) => {
                        let userInfo = res.userInfo;
                        //console.log("玩家信息："+userInfo);
                        button.hide();
                        button.destroy();
                        if (!userInfo) {
                            //this.tips.string = res.errMsg;
                            return;
                        }
            
                        // this.nickName.string = userInfo.nickName;
            
                        // cc.loader.load({url: userInfo.avatarUrl, type: 'png'}, (err, texture) => {
                        //     if (err) {
                        //         console.error(err);
                        //         return;
                        //     }
                        //     this.avatar.spriteFrame = new cc.SpriteFrame(texture);
                        // });
                        //cc.vv.Login();
                        // wx.getOpenDataContext().postMessage({
                        //     message: "User info get success."
                        // });
                        //发送消息给子域通知他展示排行榜
                        if(WECHAT){
                            miniGameApi.postMessage({ action: Consts.DomainAction.ShowFriendLevel, message: 'showFriendLevelRank' });
                        }            
            
                    });
                }
            }
        })
        this.phb.active=true;
    }
}


